<template>
  <el-dialog :visible="showDialog" title="新增员工" @close="btnCancel">
    <!-- 新增员工表单 -->
    <el-form
      ref="formData"
      label-width="120px"
      :model="formData"
      :rules="rules"
    >
      <el-form-item label="姓名" prop="username">
        <el-input
          v-model="formData.username"
          style="width: 80%"
          placeholder="1-10字符"
        ></el-input>
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input
          v-model="formData.mobile"
          style="width: 80%"
          placeholder="请输入手机号码"
        ></el-input>
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="formData.timeOfEntry"
          type="date"
          placeholder="选择日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select
          v-model="formData.formOfEmployment"
          style="width: 80%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in emploteesEnum.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input
          v-model="formData.workNumber"
          style="width: 80%"
          placeholder="1-20字符"
        ></el-input>
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          v-model="formData.departmentName"
          style="width: 80%"
          placeholder="请选择"
          @focus="getDepartmentsList"
        ></el-input>
        <!-- 放置一个tree组件 -->
        <el-tree
          v-if="treeNodeShow"
          :data="treeData"
          :props="{ label: 'name' }"
          :default-expand-all="true"
          @node-click="selectNode"
          v-loading="loading"
        >
        </el-tree>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="formData.correctionTime"
          type="date"
          placeholder="选择日期"
        >
        </el-date-picker>
      </el-form-item>
    </el-form>
    <!-- 提交 取消按钮 -->
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="8">
        <el-button size="samll" @click="btnCancel">取消</el-button>
        <el-button type="primary" size="samll" @click="btnOK">提交</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartmentsList } from "@/api/departments";
import { tranTree } from "@/utils/index";
import { addEmployees } from "@/api/employees";
import emploteesEnum from "@/api/constant/employees";
export default {
  name: "employeesDialog",
  props: {
    showDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      emploteesEnum,
      formData: {
        username: "",
        mobile: "",
        formOfEmployment: "", //聘用形式
        workNumber: "", //工号
        departmentName: "", //组织名称
        timeOfEntry: "", //入职时间
        correctionTime: "", //转正时间
      },
      rules: {
        username: [
          { required: true, message: "用户名信息不能为空", tigger: "blur" },
          {
            min: 1,
            max: 10,
            message: "用户姓名1-10位",
            tigger: "blur",
          },
        ],
        mobile: [
          {
            required: true,
            message: "用户名信息不能为空",
            tigger: "blur",
          },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: "手机号格式不正确",
            tigger: "blur",
          },
        ],
        formOfEmployment: [
          { required: true, message: "聘用形式不能为空", trigger: "blur" },
        ],
        workNumber: [
          { required: true, message: "工号不能为空", trigger: "blur" },
        ],
        departmentName: [
          { required: true, message: "部门不能为空", trigger: "change" },
        ],
        timeOfEntry: [{ required: true, message: "入职时间", trigger: "blur" }],
      },
      treeData: [],
      treeNodeShow: false,
      loading: false,
    };
  },
  methods: {
    //获取部门信息将扁形数据该树形数据
    async getDepartmentsList() {
      this.treeNodeShow = true;
      this.loading = true;
      const { depts } = await getDepartmentsList();
      this.treeData = tranTree(depts, "");
      this.loading = false;
    },
    //选择部门
    selectNode(node) {
      this.formData.departmentName = node.name;
      this.treeNodeShow = false;
    },
    //提交表单
    async btnOK() {
      try {
        await this.$refs.formData.validate();
        await addEmployees(this.formData);
        //一般不这么用
        this.$parent.getEmployees();
        this.$parent.showDialog = false;
      } catch (err) {
        console.log(err);
      }
    },
    //关闭弹层
    btnCancel() {
      this.formData = {
        username: "",
        mobile: "",
        formOfEmployment: "", //聘用形式
        workNumber: "", //工号
        departmentName: "", //组织名称
        timeOfEntry: "", //入职时间
        correctionTime: "", //转正时间
      };
      this.$refs.formData.resetFields();
      this.$emit("update:showDialog", false);
      // 'update:this.showDialog'在父组件可以 直接sync 修饰符处理
    },
  },
};
</script>

<style>
</style>